<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ include file="/WEB-INF/page/include/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

	<head>
		<meta charset="utf-8">
		<title>文章列表</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="Cache-Control" content="no-cache, must-revalidate">
		<meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57 GMT">
		<meta name="format-detection" content="telephone=no">
		<link rel="stylesheet" href="${ctx }/layui/css/layui.css" media="all" />
		<link rel="stylesheet" href="${ctx }/css/font_eolqem241z66flxr.css" media="all" />
		<link rel="stylesheet" href="${ctx }/css/list.css" media="all" />
		<script>
			var ctx = "${ctx}";
		</script>
	</head>

	<body class="childrenBody"> 
		<blockquote class="layui-elem-quote news_search">
			<form class="layui-form">
				<div>
					<div class="layui-inline">
						<div class="layui-input-inline">
							<input type="text" autocomplete="off" id="title" value="" placeholder="请输入文章标题" class="layui-input search_input">
						</div>
						<div class="layui-input-inline layui-form">
							<select name="static" class="" id="status">
								<option value="-1">请选择文章状态</option>
								<option value="1">已审核</option>
								<option value="0">未审核</option>
							</select>
						</div>
						<div class="layui-input-inline layui-form">
							<select name="cId" class="" id="cId">
								<option value="-1">请文章分类</option>
								<c:forEach items="${categoryList}" var="item">
									<option value="${item.id}">${item.category}</option>
								</c:forEach> 
							</select>
						</div>
					</div>
				</div>
				<div style="margin-top: 1%">
					<div class="layui-inline">
						<input type="text"autocomplete="off" id="createTimeStart" class="layui-input userName" name="createTimeStart" placeholder="开始时间" value="">
					</div>
					<div class="layui-inline">
						<input type="text"autocomplete="off" id="createTimeEnd" class="layui-input userName" name="createTimeEnd" placeholder="结束时间" value="">
					</div>
					<a class="layui-btn search_btn" lay-submit="" data-type="search" lay-filter="search">查询</a>
					<shiro:hasPermission name="article:article:save">
						<div class="layui-inline">
							<a class="layui-btn layui-btn-normal articleAdd_btn">添加文章</a>
						</div>
					</shiro:hasPermission>
					<shiro:hasPermission name="article:article:delete">
						<div class="layui-inline">
							<a class="layui-btn layui-btn-danger batchDel">批量删除</a>
						</div>
					</shiro:hasPermission>
					<div class="layui-inline">
						<a class="layui-btn layui-btn-warm batchAudit">批量审核</a>
					</div>
					<div class="layui-inline">（支持模糊查询）</div>
				</div>
		</blockquote>
		</form>
		<div class="layui-form">
			<table id="articleList" lay-filter="articleList"></table>
		</div>
		<script type="text/javascript" src="${ctx }/layui/layui.js"></script>
		<script type="text/html" id="barEdit">
			<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
			{{#if(d.status === 0){ }}
			<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="audit">审核</a>
			{{# } }}		
		</script>
		<script type="text/html" id="statusTpl">
			{{#if(d.status === 0){ }}
				<span style="color: #FFB800;">未审核</span>{{# } else if(d.status === 1){ }}
				<span style="color: #01AAED;">已审核</span>{{# } }}
		</script>
		<script>
		layui.config({
			base: "js/"
		}).use(['form', 'layer', 'jquery', 'table', 'laydate'],function() {
			var form = layui.form,table = layui.table;
			layer = parent.layer === undefined ? layui.layer : parent.layer,
			laypage = layui.laypage,
			laydate = layui.laydate,
			nowTime = new Date().valueOf(),
			$ = layui.jquery;
			var title = $('#title'),
			status = $("#status"),
			cId = $("#cId"),
			createTimeStart = $("#createTimeStart"),
			createTimeEnd = $("#createTimeEnd");
			var start = laydate.render({
				elem: '#createTimeStart',
				type: 'datetime',
				max: nowTime,
				btns: ['clear', 'confirm'],
				done: function(value, date) {
					endMax = end.config.max;
					end.config.min = date;
					end.config.min.month = date.month - 1;
				}
			});
			var end = laydate.render({
				elem: '#createTimeEnd',
				type: 'datetime',
				max: nowTime,
				done: function(value, date) {
					if($.trim(value) == '') {
						var curDate = new Date();
						date = {
							'date': curDate.getDate(),
							'month': curDate.getMonth() + 1,
							'year': curDate.getFullYear()
						};
					}
					start.config.max = date;
					start.config.max.month = date.month - 1;
				}
			})
			table.render({
				id: 'articleList',
				elem: '#articleList',
				url: ctx + '/article/selArticleList',
				limit: 10,
				limits: [10, 20, 30, 40],
				cols: [[
						{
							type: 'checkbox'
						},
						{
							field: 'id',
							title: 'ID',
							width: 60,
							sort: true
						},
						{
							field: 'title',
							title: '文章标题'
						},
						{
							field: 'status',
							title: '文章状态',
							templet: '#statusTpl'
						},
						{
							field: 'cId',
							title: '文章分类'
						},
						{
							field: 'authorId',
							title: '作者'
						},
						{
							field: 'createTime',
							title: '生成日期'
						},{
							field: 'updateTime',
							title: '修改日期'
						},{
							title: '操作',
							toolbar: '#barEdit'
						}
						
					    ]],
					    page: true,
						where: {
							timestamp: (new Date()).valueOf()
						}
			});
			table.on("tool(articleList)",function(obj){
				var  data = obj.data;
				if(obj.event === "del"){
					layer.confirm('真的删除行么', function(index) {
						$.ajax({
							url : ctx + '/article/deleteById',
							data:{id:data.id},									
							type : "get",
							success : function(d) {
								if (d.code == 0) {
									obj.del();
								} else {
									layer.msg(d.msg, {
										icon : 5
									});
								}
							}
						});
						layer.close(index);
					});
				}else if(obj.event === "edit"){
					 var index = layui.layer.open({
						title : "修改新闻",
						type : 2,
						area: ['100%', '100%'],
						content : ctx+"/article/editArticle/"+data.id ,
						success : function(layero, index){
							
						}
					});
				}
			});
			//条件查询事件
			$('.search_btn').on('click',function(){
				table.reload('articleList',{
					page: {curr:1},
					where:{title:title.val(),status:status.val(),cId:cId.val(),createTimeStart:createTimeStart.val(),createTimeEnd:createTimeEnd.val()}					
				});
			});
			
			$(".articleAdd_btn").on("click",function(){
				var index = layui.layer.open({
					title : "添加文章",
					type : 2,
					content : ctx+"/article/addArticle",
					success : function(layero, index){
						
					}
				})
				//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
				$(window).resize(function(){
					layui.layer.full(index);
				})
				layui.layer.full(index);
			
			});
			//批量删除
			$('.batchDel').on("click",function(){
				var checkStatus = table.checkStatus('articleList'),
				data = checkStatus.data,
				IdStr = '';
				if(data.length>0){
					$.each(data, function(n, value) {
						IdStr += value.id + ',';
					});
					IdStr = IdStr.substring(0, IdStr.length - 1);
					layer.confirm('确定删除<strong>' + data.length + '</strong>条数据吗？', function(index) {
						$.ajax({
							url: ctx + '/article/deleteAll', //接口地址
							type: "get",
							data:{IdStr:IdStr},
							success: function(d) {
								if(d.code == 0) {
									//删除成功，刷新父页面
									layer.msg("删除成功！",{icon:5});
									parent.location.reload();
								} else {
									layer.msg("权限不足，联系超管！", {
										icon: 5
									});
								}
							}
						})
					});
				}else {
					layer.msg("请选择需要删除的分类");
				}
			})
			
		})
		</script>
	
	</body>

</html>